﻿using System;
using Chat.Common;
public class CommandSender
{
    /// <summary>
    /// 向一个客户发送控制命令，如果未指定客户则向所有在线客户发送命令
    /// </summary>
    /// <param name="ctlType">规定的命令类型</param>
    /// <param name="clientId">欲发送给的客户Id</param>
    /// <param name="param">欲发送给的控制参数</param>
    public void SendControlCmd(ControlType ctlType, string clientId = null,string param=null)
    {
        string str = ctlType.ToString();
        if(String.IsNullOrEmpty(str))
            throw new Exception("ctlType is null or empty……");
        if (str.Length > Common.ctlTypeSize)
            throw new Exception("ctlType length overflow……");
        else
        {
            str = str + Common.GetLoopStr(" ", Common.ctlTypeSize - str.Length);
            if(string.IsNullOrEmpty(clientId))
                    Send(Common.rcvDataHead, CommandType.Control, str + param, Common.rcvDataTail);
            else
                Send(Common.rcvDataHead, CommandType.Control, str + param, Common.rcvDataTail,clientId);
        }
    }
    /// <summary>
    /// 向指定聊天对象发送一个聊天命令
    /// </summary>
    /// <param name="chatType">规定的聊天类型,群聊&私聊</param>
    /// <param name="from">发送者Id</param>
    /// <param name="to">接收者Id</param>
    /// <param name="msg">欲发送的消息</param>
    public void SendChatCmd(ChatType chatType, string from ,string to,string msg)
    {
        string cmd = chatType.ToString();
        if (String.IsNullOrEmpty(cmd))
            throw new Exception("chatType is null or empty……");
        if (cmd.Length > Common.chtTypeSize)
            throw new Exception("chatType length overflow……");
        else
        {
            cmd = cmd + Common.GetLoopStr(" ", Common.chtTypeSize - cmd.Length);
            cmd = cmd + from +to + msg;
            if (chatType == ChatType.Public)
                SendToRoom(Common.rcvDataHead, CommandType.Chat, cmd, Common.rcvDataTail, to);
            else if (chatType == ChatType.Private)
                Send(Common.rcvDataHead, CommandType.Chat, cmd, Common.rcvDataTail, to);
            else
                return;
        }
    }
    /// <summary>
    /// 向一个客户发送命令,如果未指定客户则向所有在线客户发送命令
    /// </summary>
    /// <param name="cmdHead">规定的命令头</param>
    /// <param name="cmdType">规定的命令类型</param>
    /// <param name="cmd">规定的命令</param>
    /// <param name="cmdTail">规定的命令尾</param>
    /// <param name="clientId">欲发送给的客户Id</param>
    public void Send(string cmdHead, CommandType cmdType, string cmd, string cmdTail, string clientId=null)
    {
        string strCmdType = cmdType.ToString();
        if (cmdHead.Length > Common.cmdHeadSize)
            throw new Exception("The cmdHead length overflow……");
        if (strCmdType.Length > Common.cmdTypeSize)
            throw new Exception("The cmdType length overflow……");
        if (cmdTail.Length > Common.cmdTailSize)
            throw new Exception("The cmdTail length overflow……");

        if (cmdHead.Length < Common.cmdHeadSize)
            cmdHead = cmdHead + Common.GetLoopStr(" ", Common.cmdHeadSize - cmdHead.Length);
        if (strCmdType.Length < Common.cmdTypeSize)
            strCmdType = strCmdType + Common.GetLoopStr(" ", Common.cmdTypeSize - strCmdType.Length);
        if (cmdTail.Length < Common.cmdTailSize)
            cmdTail = cmdTail + Common.GetLoopStr(" ", Common.cmdTailSize - cmdTail.Length);

        string strCmd = cmdHead + strCmdType + cmd + cmdTail;

        if (string.IsNullOrEmpty(clientId))
            foreach (Member member in Singleton<MemberManager>.GetInstance().Members)
                member.Send(strCmd);
        else
            Singleton<MemberManager>.GetInstance().GetValue(clientId).Send(strCmd);
    }
    /// <summary>
    /// 向一个房间发送命令,如果未指定房间则向所有在房间客户发送命令
    /// </summary>
    /// <param name="cmdHead">规定的命令头</param>
    /// <param name="cmdType">规定的命令类型</param>
    /// <param name="cmd">规定的命令</param>
    /// <param name="cmdTail">规定的命令尾</param>
    /// <param name="roomId">欲发送给的房间Id</param>
    public void SendToRoom(string cmdHead, CommandType cmdType, string cmd, string cmdTail,string roomId=null)
    {
        if (string.IsNullOrEmpty(roomId))
        {
            foreach (var room in Singleton<RoomManager>.GetInstance().Rooms)
                foreach (var member in room.Members)
                    Send(cmdHead, cmdType, cmd, cmdTail, member.Id);
        }
        else
        {
            if(Singleton<RoomManager>.GetInstance().IsExist(roomId))
                foreach (var member in Singleton<RoomManager>.GetInstance().GetValue(roomId).Members)
                    Send(cmdHead, cmdType, cmd, cmdTail, member.Id);
        }
    }
}
